package infostroy.bookagolf.views;

import com.academysmart.bookagolf.i18n.Messages;
import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.common.Constants.Dimensions;
import infostroy.bookagolf.common.Constants.Icons;
import infostroy.bookagolf.common.helpers.ImageHelper;
import infostroy.bookagolf.components.inputs.GolfComboBox;
import infostroy.bookagolf.components.panels.BaseDraggablePanel;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;


public class HeaderPanel extends BaseDraggablePanel implements Colors, Dimensions{

    private static final String HELP = Messages.MainWindow_Help;
    private static final String ABOUT = Messages.MainWindow_About;

    //Control buttons
    private JButton minimizeBtn;
    private JButton maximizeBtn;
    private JButton closeBtn;
    //Control buttons icons
    private Image minimizeIcon;
    private Image maximizeIcon;
    private Image closeIcon;
    //Text labels
    //TODO(Valera) : read values from property file
    private JLabel aboutLbl = new JLabel(ABOUT);
    private JLabel helpLbl = new JLabel(HELP);

    private BufferedImage logo;

    private GolfComboBox localizationJCB;

    public HeaderPanel(Container window) {
        super(window);
        logo = ImageHelper.getInstance().loadImage(Icons.LOGO);
        minimizeIcon = ImageHelper.getInstance().loadImage(Icons.MINIMIZE);
        maximizeIcon = ImageHelper.getInstance().loadImage(Icons.MAXIMIZE);
        closeIcon = ImageHelper.getInstance().loadImage(Icons.CLOSE);
//        localizationJCB = new GolfComboBox(Localizations.values());
//        localizationJCB.setPreferredSize(new Dimension(55,30));
//        localizationJCB.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                Locale l = Locale.getDefault();
//                Locale ruLocale = new Locale.Builder().setLanguage("ru").setRegion("RU").build();
//                String s = ResourceBundle.getBundle("com.academysmart.bookagolf.i18n.messages").getString("PlayerName");
//                if(((Localizations)localizationJCB.getSelectedItem()).getValue().equals("EN")){
//                    Locale.setDefault(Locale.ENGLISH);
//                    ResourceBundle.clearCache();
//                }else{
//                    Locale.setDefault(ruLocale);
//                    ResourceBundle.clearCache();
//                }
//            }
//        });

        addComponents();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        graphics.setColor(WHITE);
        graphics.fill(new Rectangle(0, 0, HEADER_DIM.width, HEADER_DIM.height));
        //paint gradient
        GradientPaint gradient = new GradientPaint(0, 0, HEADER_TOP_COL, 0, HEADER_DIM.height - 1, HEADER_BOT_COL);
        graphics.setPaint(gradient);
        graphics.fill(new Rectangle(0, 3, HEADER_DIM.width, HEADER_DIM.height));
        //paint logo
        graphics.drawImage(logo, LOGO_PADDING_DIM.width
                , LOGO_PADDING_DIM.height, null);
        //paint bottom line
        graphics.setColor(HEADER_LINE_COL);
        graphics.drawLine(0, HEADER_DIM.height - 1, HEADER_DIM.width, HEADER_DIM.height - 1);
    }

    @Override
    public Dimension getPreferredSize() {
        return HEADER_DIM;
    }

    private void addComponents() {
        setLayout(new FlowLayout(FlowLayout.TRAILING));
        setBorder(new EmptyBorder(15, 0, 0, 15));

        aboutLbl.setFont(new Font("Arial", Font.ROMAN_BASELINE, 15));
        add(aboutLbl);

        helpLbl.setFont(new Font("Arial", Font.ROMAN_BASELINE, 15));
        add(helpLbl);
//        add(localizationJCB);
        //initing buttons
        minimizeBtn = new JButton(new ImageIcon(minimizeIcon));
        minimizeBtn.setPreferredSize(new Dimension(minimizeIcon.getWidth(null)
                , minimizeIcon.getHeight(null)));
        minimizeBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (getWindow() instanceof JFrame) {
                    JFrame frame = (JFrame) getWindow();
                    frame.setState(JFrame.ICONIFIED);
                }
            }
        });
        add(minimizeBtn);

        maximizeBtn = new JButton(new ImageIcon(maximizeIcon));
        maximizeBtn.setPreferredSize(new Dimension(maximizeIcon.getWidth(null)
                , maximizeIcon.getHeight(null)));
        maximizeBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //TODO:(Valera) : maximize window
            }
        });
        add(maximizeBtn);

        closeBtn = new JButton(new ImageIcon(closeIcon));
        closeBtn.setPreferredSize(new Dimension(closeIcon.getWidth(null)
                , closeIcon.getHeight(null)));
        closeBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
        });
        add(closeBtn);
    }
}
